package com.ccut.xunfeiSpringboot.utils;/*
 * ClassName: DtoToMapConverter
 * Package: com.ccut.xunfeiSpringboot.utils
 * Description:统一处理 DTO → Map 的转换。
 * @Author 刘千湫
 * @Create 2025/6/18 19:23
 * @Version 1.0
 */


import com.ccut.xunfeiSpringboot.dto.SchoolResumeDTO;
import com.ccut.xunfeiSpringboot.dto.StudentResumeDTO;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import java.util.HashMap;
import java.util.Map;

public class DtoToMapConverter {

    public static Map<String, String> convertStudent(StudentResumeDTO dto) {
        return convert(dto);
    }

    public static Map<String, String> convertSchool(SchoolResumeDTO dto) {
        return convert(dto);
    }

    private static <T> Map<String, String> convert(T dto) {
        Map<String, String> map = new HashMap<>();
        BeanWrapper wrapper = new BeanWrapperImpl(dto);
        for (var p : wrapper.getPropertyDescriptors()) {
            if (!"class".equals(p.getName())) {
                Object value = wrapper.getPropertyValue(p.getName());
                if (value != null) {
                    map.put(p.getName(), value.toString());
                }
            }
        }
        return map;
    }
}
